from django.core.files.storage import FileSystemStorage
from fdfs_client.client import Fdfs_client


class FdfsStorage(FileSystemStorage):
    """自定义文件存储系统类"""

    def _save(self, name, content):
        """

        :param name:
        :param content:  内容
        :return:  自动保存到url数据库中
        """
        client = Fdfs_client('meiduo_mall/utils/fastdfs/client.conf')
        dict_data = client.upload_by_buffer(content.read())

        status = dict_data.get('Status')
        if status != 'Upload successed.':
            raise Exception('上传文件到FastDFS失败!!!')

        path = dict_data.get('Remote file_id')
        return super()._save(name, content)

